package com.example.spinnertest.tools;

import java.util.ArrayList;
import java.util.List;

import com.example.spinnertest.R;
import com.example.spinnertest.R.id;
import com.example.spinnertest.R.layout;


import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;

public class SpinnerPopWindow extends PopupWindow implements OnItemClickListener{
	private ListView lv = null;
	private Context context ;
		
	private SpinnerAdapter spAdapter = null;
	

	private List nameList = new ArrayList();
	
	public static interface IOnItemSelectListener
	{
		public void onItemClick(int position);
	}
	//接口IOnItemClickListenr
	private IOnItemSelectListener iOnItemSelectListener;
	public SpinnerPopWindow(Context context) {
		super(context);
		this.context = context;
		init();
	}
	
	
	//给listview+适配器
	public void setSpinnerAdapter(String [] str)
	{
		spAdapter = new SpinnerAdapter(context);
		for(int i=0;i<str.length;i++)
		{
			nameList.add(str[i]);
		}
		
		spAdapter.refreshData(nameList);
		if(spAdapter!=null)
		{
			spAdapter.notifyDataSetChanged();
		}
		lv.setAdapter(spAdapter);
		
	}
	
	public void setItemSelectListener(IOnItemSelectListener iOnItemSelectListener)
	{
		this.iOnItemSelectListener = iOnItemSelectListener;
	}
	
	//初始化listview
	public void init()
	{
		View view = LayoutInflater.from(context).inflate(R.layout.spinner_window_layout	, null);
		setContentView(view);
		setWidth(LayoutParams.WRAP_CONTENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		setFocusable(true);
		
		lv = (ListView) view.findViewById(R.id.listview);
		//点击
		lv.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
	  dismiss();
	  if (iOnItemSelectListener!=null)
	  {
		  iOnItemSelectListener.onItemClick(position);
	  }
	}
	
}
